package frontend.model.admin;

import backend.model.admin.*;
import backend.model.other.User;
import shared.other.UserRolesDefinition.UserRole;
import backend.service.admin.TeamService;
import frontend.core.TimeTrackingWebApplication;
import frontend.model.base.UIModel;
import org.apache.commons.lang.StringUtils;
import shared.util.MD5CryptographyUtil;

public class EmployeeUIModel implements UIModel<Employee> {
    private static final String DEFAULT_PASSWORD = "temp";

    private String firstName;
    private String lastName;
    private String identificationCard;
    private boolean isApplicationUser;
    private String username;
    private String password;
    private UserRole role;
    private boolean isForeman;
    private boolean isActive;

    private Employee employee;
    private Team team;
    private Occupation occupation;
    private Team defaultTeam = TimeTrackingWebApplication.getService(TeamService.class).findDefaultTeam();;

    public EmployeeUIModel() {
        employee = new Employee();
        isActive = true;
        team = defaultTeam;
    }

    public EmployeeUIModel(Employee employee) {
        this.employee = employee;
        populateFieldsFromDBModel();
    }

    @Override
    public void populateFieldsFromDBModel() {
        firstName = employee.getFirstName();
        lastName = employee.getLastName();
        identificationCard = employee.getIdentificationCard();
        isActive = employee.getIsActive();
        if (employee.getOccupation() != null) {
            occupation = employee.getOccupation();
        }
        if (employee.getTeam() != null) {
            team = employee.getTeam();
        }
        if (employee.getUser() != null && employee.getUser().getIsActive()) {
            isApplicationUser = true;
            username = employee.getUser().getUsername();
            password = employee.getUser().getPassword();
            role = employee.getUser().getRole();
        }
        if (employee.getForeman() != null && employee.getForeman().getIsActive()) {
            isForeman = true;
        }
    }

    @Override
    public void populateDBModelFromFields() {
        employee.setFirstName(firstName);
        employee.setLastName(lastName);
        employee.setIdentificationCard(identificationCard.toUpperCase());
        employee.setOccupation(occupation);
        employee.setIsActive(isActive);
        if (!isActive) {
            employee.setTeam(defaultTeam);
        } else {
            employee.setTeam(team);
        }

        if (isApplicationUser) {
            if (employee.getUser() == null) {
                employee.setUser(new User());
                employee.getUser().setIsActive(true);
                employee.getUser().setEmployee(employee);
            }
            employee.getUser().setUsername(username);
            if (StringUtils.isEmpty(password)) {
                password = MD5CryptographyUtil.encrypt(DEFAULT_PASSWORD);
            }
            employee.getUser().setPassword(password);
            employee.getUser().setRole(role);
        }
        else if (employee.getUser() != null && !isApplicationUser) {
            employee.getUser().setIsActive(false);
        }

        if (employee.getForeman() == null && isForeman) {
            employee.setForeman(new Foreman());
            employee.getForeman().setEmployee(employee);
            employee.getForeman().setIsActive(true);
        } else if (employee.getForeman() != null && !isForeman){
            employee.getForeman().setIsActive(false);
        }
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getIdentificationCard() {
        return identificationCard;
    }

    public Occupation getOccupation() {
        return occupation;
    }

    public Team getTeam() {
        return team;
    }

    public boolean getIsApplicationUser() {
        return isApplicationUser;
    }

    public boolean getIsForeman() {
        return isForeman;
    }

    public String getUsername() {
        return username;
    }

    public UserRole getRole() {
        return role;
    }

    @Override
    public Employee getDBModel() {
        return employee;
    }
}
